package com.jc.utils;

import org.springframework.beans.BeanUtils;

public class PageUtil {
    /**
     * 分页默认常量: 分页页码
     */
    public static final Integer DEFAULT_PAGEINDEX = 1;

    /**
     * 分页默认常量: 分页尺寸1
     */
    public static final Integer DEFAULT_PAGESIZE = 10;

    /**
     * 分页默认常量: 分页尺寸2
     */
    public static final Integer DEFAULT_PAGESIZE_2 = 15;

    /**
     * 分页默认常量: 分页尺寸上限
     */
    public static final Integer DEFAULT_PAGESIZE_UP_LIMIT = 1000;

    /**
     * 分页默认常量: 分页尺寸下限
     */
    public static final Integer DEFAULT_PAGESIZE_LOW_LIMIT = 1;

    /**
     * 分页默认常量: 是否需要分页
     */
    public static final Boolean DEFAULT_IS_PAGED = true;

    /**
     * 是否需要分页
     */
    private Boolean isPaged = DEFAULT_IS_PAGED;

    /**
     * 页码
     */
    private Integer pageIndex = DEFAULT_PAGEINDEX;

    /**
     * 页面尺寸
     */
    private Integer pageSize = DEFAULT_PAGESIZE;

    /**
     * 总记录条数
     */
    private Integer recordCount = 0;

    public Integer getPageOffset() {
        return getFirstPage();
    }

    public Boolean getIsPaged() {
        return isPaged;
    }

    public void setIsPaged(Boolean isPaged) {
        this.isPaged = (null == isPaged ? DEFAULT_IS_PAGED : isPaged);
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public PageUtil setPageIndex(Integer pageIndex) {
        this.pageIndex = (null == pageIndex || pageIndex < DEFAULT_PAGEINDEX ? DEFAULT_PAGEINDEX : pageIndex);
        return this;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public PageUtil setPageSize(Integer pageSize) {
        this.pageSize = (null == pageSize ? DEFAULT_PAGESIZE : pageSize);
        this.pageSize = (this.pageSize < DEFAULT_PAGESIZE_LOW_LIMIT ? DEFAULT_PAGESIZE_LOW_LIMIT : this.pageSize);
        this.pageSize = (this.pageSize > DEFAULT_PAGESIZE_UP_LIMIT ? DEFAULT_PAGESIZE_UP_LIMIT : this.pageSize);
        return this;
    }

    public Integer getRecordCount() {
        return recordCount;
    }

    public void setRecordCount(Integer recordCount) {
        this.recordCount = recordCount;
    }

    /**
     * 获得当前分页对象的分页起始页
     *
     * @return
     */
    public Integer getFirstPage() {
        if (null == pageIndex || null == pageSize) {
            return 0;
        }
        return (pageIndex - 1) * pageSize;
    }

    /**
     * 获得分页起始页
     *
     * @param pageIndex
     *            分页页码
     * @param pageSize
     *            分页尺寸
     * @return
     */
    public static Integer getFirstPage(Integer pageIndex, Integer pageSize) {
        if (null == pageIndex || null == pageSize) {
            return 0;
        }
        return (pageIndex - 1) * pageSize;
    }

    /**
     * 获得当前分页对象的分页末页
     *
     * @return
     */
    public Integer getLastPage() {
        if (null == pageSize) {
            return this.getFirstPage();
        }
        return this.getFirstPage() + pageSize;
    }

    /**
     * 获得分页末页
     *
     * @param pageIndex
     *            分页页码
     * @param pageSize
     *            分页尺寸
     * @return
     */
    public static Integer getLastPage(Integer pageIndex, Integer pageSize) {
        if (null == pageSize) {
            return 0;
        }
        return PageUtil.getFirstPage(pageIndex, pageSize) + pageSize;
    }

    /**
     * 获得当前分页对象的总页数
     *
     * @return
     */
    public Integer getPageNum() {
        if (null == recordCount || null == pageSize || pageSize == 0 ) {
            return 0;
        }
        int pageNum = recordCount / pageSize;
        // 如果不能整除，页数加1
        if ((recordCount % pageSize) != 0) {
            pageNum++;
        }
        return pageNum;
    }

    /**
     * 获得总页数
     *
     * @param pageSize
     *            分页尺寸
     * @param recordCount
     *            总记录数
     * @return
     */
    public static Integer getPageNum(Integer recordCount, Integer pageSize) {
        if (null == recordCount || null == pageSize || pageSize == 0) {
            return 0;
        }

        int pageNum = recordCount / pageSize;
        // 如果不能整除，页数加1
        if ((recordCount % pageSize) != 0) {
            pageNum++;
        }
        return pageNum;
    }

    /**
     * 是否到达尾页
     *
     * @param pageIndex
     *            分页页码
     * @return
     */
    public boolean getToTheEnd(Integer pageIndex) {
        if (null == pageIndex) {
            return false;
        }
        return pageIndex >= this.getPageNum();
    }

    /**
     * 是否到达尾页
     *
     * @param pageIndex
     *            分页页码
     * @param recordCount
     *            总记录数
     * @param pageSize
     *            分页尺寸
     * @return
     */
    public boolean getToTheEnd(Integer pageIndex, Integer recordCount, Integer pageSize) {
        if (null == pageIndex) {
            return false;
        }
        return pageIndex >= getPageNum(recordCount, pageSize);
    }

    /**
     * 回到首页
     */
    public void backToTheBeginning() {
        this.pageIndex = DEFAULT_PAGEINDEX;
    }

    /**
     * 对现有的分页对象做校验，不符合条件则设定默认值
     *
     * @param page
     * @return
     */
    public static PageUtil verifyToSetDefaultPageValues(PageUtil page) {
        if (null == page) {// 如果null 则取默认
            return new PageUtil();
        }

        PageUtil newPage = new PageUtil();
        BeanUtils.copyProperties(page, newPage);//使用spring的BeanUtils
        if (null == page.getIsPaged()) {
            newPage.setIsPaged(true);
        }
        if (null == page.getPageIndex() || page.getPageIndex() < DEFAULT_PAGEINDEX) {
            newPage.setPageIndex(DEFAULT_PAGEINDEX);
        }
        if (null == page.getPageSize()) {
            newPage.setPageSize(DEFAULT_PAGESIZE);
        }
        if (page.getPageSize() < DEFAULT_PAGESIZE_LOW_LIMIT) {
            newPage.setPageSize(DEFAULT_PAGESIZE_LOW_LIMIT);
        }
        if (page.getPageSize() > DEFAULT_PAGESIZE_UP_LIMIT) {
            newPage.setPageSize(DEFAULT_PAGESIZE_UP_LIMIT);
        }
        return newPage;
    }

    /**
     * 获取默认分页对象
     *
     * @return
     */
    public static PageUtil getDefaultPage() {
        return new PageUtil();
    }
}
